package com.linjiahao.servicebase.common;


import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ResultMap
 * @Description: 统一返回结果格式
 * @Author linjiahao
 * @Date 29/12/2021
 **/
@Data
public class ResultMap {

    private Boolean success;

    private Integer code;

    private String message;

    private Map<String, Object> data = new HashMap<String, Object>();

    public ResultMap() {
    }

    public static ResultMap ok() {
        ResultMap r = new ResultMap();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    public static ResultMap error() {
        ResultMap r = new ResultMap();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失败");
        return r;
    }

    public static ResultMap error(Integer code, String message) {
        ResultMap r = new ResultMap();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public ResultMap success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public ResultMap message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResultMap code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResultMap data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public ResultMap data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
